package cn.buk.qms.web;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.tms.entity.misc.OperationLog;
import cn.buk.tms.sc.SearchFlightOrderCriteria;
import cn.buk.tms.vas.dto.request.VasOrderRequestDto;
import cn.buk.tms.vas.service.VasOrderService;
import cn.buk.tms.vas.dto.response.VasOrderResponseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author yfdai
 */
@RestController
@RequestMapping("/order")
public class OrderVasController extends BaseController {

    private final VasOrderService vasOrderService;

    @Autowired
    public OrderVasController(VasOrderService vasOrderService) {
        this.vasOrderService = vasOrderService;
    }

    /**
     * 创建增值服务订单
     */
    @PostMapping(value = "/vas/new", consumes = "application/json")
    public JsonResult createVasOrder(HttpServletRequest request,
                                             @RequestBody VasOrderRequestDto dto) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return vasOrderService.createVasOrder(enterpriseId, username, dto);
    }


    /**
     * 查找vas(保险）订单
     */
    @RequestMapping("/insurance")
    public CommonDto<VasOrderResponseDto> searchInsuranceOrders(HttpServletRequest request,
                                                  @ModelAttribute("sc") SearchFlightOrderCriteria sc
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final int userId = getUserId(request);

        List<VasOrderResponseDto> datas = vasOrderService.searchInsuranceOrders(enterpriseId, userId, sc);

        CommonDto<VasOrderResponseDto> commonDto = new CommonDto<>();
        commonDto.setDataList(datas);
        commonDto.setPage(sc.getPage());

        return commonDto;
    }

    /**
     * 查找vas(非保险）订单
     */
    @RequestMapping("/vasOrders")
    public CommonDto<VasOrderResponseDto> searchVasOrders(HttpServletRequest request,
                                                  @ModelAttribute("sc") SearchFlightOrderCriteria sc
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);
        final int userId = getUserId(request);

        return vasOrderService.searchVasOrders2(enterpriseId, userId, username, sc);
    }

    /**
     * 获取vas订单详情
     */
    @RequestMapping("/vas/{id}")
    public VasOrderResponseDto getVasOrderDetail(HttpServletRequest request,
                                            @PathVariable("id") int id
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final int userId = getUserId(request);

        return vasOrderService.getVasOrderById(enterpriseId, userId, id);
    }

    /**
     * 获取订单操作记录
     */
    @RequestMapping("/vas/{orderId}/history")
    public List<OperationLog> searchVasOrderHistory(HttpServletRequest request,
                                                    @PathVariable("orderId") int orderId
    ) {
        final int enterpriseId = getEnterpriseId(request);

        return vasOrderService.searchVasOrderHistory(enterpriseId, orderId);
    }

    /**
     * 修改订单备注
     */
    @RequestMapping("/vas/{orderId}/remark")
    public JsonResult updateVasOrderRemark(HttpServletRequest request,
                                                   @PathVariable("orderId") int orderId,
                                                  @RequestParam("remark") String remark
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return vasOrderService.updateVasOrderRemark(enterpriseId, username, orderId, remark);
    }

    /**
     * 修改订单的供应商及支出支付方式
     */
    @PostMapping(value = "/vas/{orderId}/supplier")
    public JsonResult updateVasOrderSupplier(HttpServletRequest request,
                                                @PathVariable("orderId") int orderId,
                                                @RequestParam("supplierId") int supplierId,
                                             @RequestParam(value = "paymentMethodId", defaultValue = "0") int paymentMethodId
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return vasOrderService.updateVasOrderSupplier(enterpriseId, username, orderId, supplierId, paymentMethodId);
    }

    /**
     * 修改订单的支付方式
     */
    @PostMapping(value = "/vas/{orderId}/payment")
    public JsonResult updateVasOrderPayment(HttpServletRequest request,
                                                     @PathVariable("orderId") int orderId,
                                                     @RequestParam("paymentMethodId") int paymentMethodId
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return vasOrderService.updateVasOrderPayment(enterpriseId, username, orderId, paymentMethodId);
    }

    /**
     * 修改订单的价格
     */
    @PostMapping(value = "/vas/{orderId}/price")
    public JsonResult updateVasOrderPrice(HttpServletRequest request,
                                             @PathVariable("orderId") int orderId,
                                             @RequestParam("price") double price,
                                             @RequestParam("discount") double discount,
                                             @RequestParam("cost") double cost
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return vasOrderService.updateVasOrderPrice(enterpriseId, username, orderId, price, discount, cost);
    }

    /**
     * 取消订单
     */
    @RequestMapping("/vas/{orderId}/status/canceled")
    public JsonResult cancelVasOrder(HttpServletRequest request,
                                           @PathVariable("orderId") int orderId,
                                           @RequestParam("remark") String remark
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return vasOrderService.cancelVasOrder(enterpriseId, username, orderId, remark);
    }

    /**
     * 开始处理订单
     */
    @RequestMapping("/vas/{orderId}/status/processing")
    public JsonResult processVasOrder(HttpServletRequest request,
                                     @PathVariable("orderId") int orderId
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return vasOrderService.processVasOrder(enterpriseId, username, orderId);
    }

    /**
     * 订单完成了
     */
    @RequestMapping("/vas/{orderId}/status/finished")
    public JsonResult finishVasOrder(HttpServletRequest request,
                                      @PathVariable("orderId") int orderId,
                                     @RequestParam(value = "remark", defaultValue = "") String remark
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return vasOrderService.finishVasOrder(enterpriseId, username, orderId, remark);
    }

    /**
     * 修改客人对应的信息
     */
    @PostMapping(value = "/vas/{orderId}/passenger")
    public JsonResult updateVasOrderPsg(HttpServletRequest request,
                                           @PathVariable("orderId") int orderId,
                                           @RequestParam("id") int psgId,
                                           @RequestParam("name") String psgName,
                                           @RequestParam("idNo") String psgIdNo,
                                           @RequestParam("remark") String psgRemark
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return vasOrderService.updateVasOrderPassenger(enterpriseId, username, orderId, psgId, psgName, psgIdNo, psgRemark);
    }

    /**
     * 修改订单为已付款
     */
    @PostMapping(value = "/vas/{orderId}/paid")
    public JsonResult updateVasOrderPaid(HttpServletRequest request,
                                         @PathVariable("orderId") int orderId,
                                        @RequestParam("remark") String remark
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return vasOrderService.updateVasOrderPaid(enterpriseId, username, orderId, remark);
    }
}
